package org.jeecg.modules.bpm.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.repository.Model;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bpm.dto.ModelDTO;
import org.jeecg.modules.bpm.service.impl.ActModelService;
import org.jeecg.modules.extbpm.util.CommonRandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 流程设计管理
 * 
 * @author scott
 * @date 2019/03/06
 * @desc
 */
@Slf4j
@RestController("act/model")
@RequestMapping("/act/model")
public class ActModelController {
	@Autowired
	private ActModelService actModelService;
	
	/**
	 * 流程设计模型列表
	 * @param params
	 * @return
	 */
	@GetMapping("list")
	public Result<IPage<Model>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		Result<IPage<Model>> result = new Result<IPage<Model>>();
		IPage<Model> pageList = new Page<Model>();
		List<Model> records = actModelService.page(params);
		pageList.setRecords(records);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 * 创建模型
	 */
	@RequestMapping("/create")
	public Result<String> create(@RequestBody ModelDTO dto, HttpServletRequest request, HttpServletResponse response) {
		Result<String> result = new Result<String>();
		try {
			String modelId = actModelService.save(dto.getName(), dto.getKey(), dto.getDescription());
			result.setResult(modelId);
		    //response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("新建流程失败！");
		}
		return result;
	}


	/**
	 * 流程发布
	 * @param id
	 * @return
	 */
	@GetMapping("deploy/{id}")
	public Result deploy(@PathVariable("id") String id) {
		actModelService.deploy(id);
		return Result.ok("流程发布成功");
	}
	
	/**
	 * 流程导出
	 * @param id
	 * @param response
	 * @return
	 */
	@GetMapping("export/{id}")
	public Result<Object> export(@PathVariable("id") String id, @ApiIgnore HttpServletResponse response) {
		try {
			actModelService.export(id, response);
			return Result.ok("导出成功！");
		} catch (Exception e) {
			return Result.error("导出失败："+e.toString());
		}
	}

	/**
	 * 删除流程设计模型
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result delete(@RequestParam(name = "ids", required = true) String ids) {
		if(CommonRandomUtil.isNotEmpty(ids)) {
			for (String id : Arrays.asList(ids.split(","))) {
				actModelService.delete(id);
			}
		}
		return Result.ok("流程删除成功");
	}
}